#include "weapon_choice.h"
#include "button.h"
#include "graphics.h"
#include "images_diffuse.h"
#include "tag.h"

#define GLFW_KEY_ENTER   257

extern entity *player;

declare_body(weapon_choice, {
	int index;
	int screen_index;
})

static int current_screen_index = 0;

declare_tick(weapon_choice, {
	if(!current_screen_index) return 1;
	
	float start = 100 - current_screen_index * 12 - (current_screen_index - 1) * 1;
	parent->position.y = 10;
	parent->position.x = start + 26 * self->screen_index;
	
	if(on_down(get_key('1'))) {
		if(self->screen_index == 0) player_load_weapon(player, self->index);
		current_screen_index = 0;
		return 1;
	}
	if(on_down(get_key('2')) && self->screen_index > 0) {
		if(self->screen_index == 1) player_load_weapon(player, self->index);
		current_screen_index = 0;
		return 1;
	}
	if(on_down(get_key('3')) && self->screen_index > 1) {
		if(self->screen_index == 2) player_load_weapon(player, self->index);
		current_screen_index = 0;
		return 1;
	}
	if(on_down(get_key(GLFW_KEY_ENTER))) {
		current_screen_index = 0;
		return 1;
	}
})

declare_draw(weapon_choice, { })
declare_head(weapon_choice)

entity *make_weapon_choice(int frame) {
	if(current_screen_index > 2) return NULL;
	
	entity *ent = ent_new();
	ent->tag = TAG_WEAPON_CHOICE;
	
	sprite_renderer *sr = get_renderer(&sprite_weapon_choice);
	sr->frame = frame;
	
	weapon_choice *wc = c_new(weapon_choice);
	wc->index = frame;
	wc->screen_index = current_screen_index++;
	
	add_component(ent, sr);
	add_component(ent, wc);
	
	return ent;
}